/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Koneksi;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import oracle.jdbc.OracleDriver;
import oracle.jdbc.pool.OracleDataSource;
import sigajikaryawan.Login;

/**
 *
 * @author yolan
 */
public class KoneksiDatabase {
private static String jdbcURL = "jdbc:Oracle:thin:@172.23.9.185:1521:orcl";
    private static String user = "MHS105314029";
    private static String password = "MHS105314029";
//    public static String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:xe";
//    public static String userid = "youland";
//    public static String password = "youland";
    public static Connection conn;
    Statement stmt;
    ResultSet rset;
    String query;
    String sqlString;

//        forName(oracle.jdbc.driver.OracleDriver);
    public KoneksiDatabase() {
    }

    public static Connection getConn() {
        try {
            
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcURL);
            conn = (Connection) ds.getConnection(user, password);
            System.out.println("Koneksi berhasil");
            return conn;
        } catch (SQLException ex) {
            System.out.println("Masih belum terkoneksi");
            return null;
        }
    }

    public void close() {
        try {
            conn.close();
        } catch (SQLException ex) {
            System.out.println("Tidak bisa tutup koneksi");

        }

    }

    @SuppressWarnings("static-access")
    public static void main(String[] args) throws SQLException {
        KoneksiDatabase datahandler = new KoneksiDatabase();
        datahandler.getConn();
        datahandler.close();
    }
    public static  boolean getConnection(Login lg) {
        Connection conn = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(KoneksiDatabase.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            String url = "jdbc:Oracle:thin:@172.23.9.185:1521:orcl";
            conn = DriverManager.getConnection(url, lg.getNip(), lg.getPassword());
            System.out.println("Koneksi Berhasil");
            return true;
        } catch (SQLException ex) {
            System.out.println("Koneksi gagal");
            Logger.getLogger(KoneksiDatabase.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }

    }
//        private String jdbcURL = "jdbc:Oracle:thin:@172.23.9.185:1521:orcl";
//    private String user = "MHS105314037";
//    private String password = "MHS105314037";
//    private Connection conn = null;
//
//    public KoneksiDatabase() {
//        try {
//            DriverManager.registerDriver(new OracleDriver());
//            conn = DriverManager.getConnection(jdbcURL, user, password);
//            System.out.println("Koneksi Berhasil");
//        } catch (Exception ex) {
//            System.out.println("Message :" + ex.getMessage());
//        }
//    }
//
//    public static void main(String[] args) {
//        KoneksiDatabase koneksiDatabase = new KoneksiDatabase();
//        try {
//            String query = "select * from databarang";
//            Statement statement = koneksiDatabase.getConn().createStatement();
//            ResultSet resultSet = statement.executeQuery(query);
//
//            while (resultSet.next()) {
//                System.out.println("kode Kategori :" + resultSet.getString("kodeKategori"));
//                System.out.println("nama Kategori :" + resultSet.getString("namaKategori"));
//
//            }
//            statement.close();
//        } catch (SQLException ex) {
//            System.out.println("Message : " + ex.getMessage());
//        }
//    }
//    public Connection getConn() {
//        return conn;
//    }
//
//    /**
//     * @param conn the conn to set
//     */
//    public void setConn(Connection conn) {
//        this.conn = conn;
//    }
}
